<?php


namespace app\common\service;

use addons\epay\library\Service;
use think\Db;


class Pay
{
  protected $order;
  protected $cancel;
  protected $type;

  public static function refund($order,$desc, $money){
    if($order['refund_status']==1){
      return false;
    }

    if($order['payment_type']=='wechat'){
      $config=Service::getConfig('wechat');
      $pay=\Yansongda\Pay\Pay::wechat($config);
      $refund_order = [
        'out_trade_no'  => $order['order_sn'],
        'out_refund_no' => 'R'.$order['order_sn'].'-'.time(),
        'total_fee'     => $order['order_amount'] * 100,
        'refund_fee'    => $money?$money*100:$order['order_amount'] * 100,
        'refund_desc'   => $desc,
      ];
    } else if($order['payment_type']=='alipay'){
      $refund_order = [
        'out_trade_no' => $order->order_sn,
        'refund_amount' => $money?:$order['order_amount'],
        'out_request_no'=>$order->order_sn
      ];
      $config=Service::getConfig('alipay');
      $pay=\Yansongda\Pay\Pay::alipay($config);
    }
    $refund = $pay->refund($refund_order);
    $refund = json_decode($refund, true);
    adlog('退款', $refund);
    if ($refund && ((isset($refund['return_code']) && $refund['return_code'] == 'SUCCESS')||(isset($refund['msg']) && $refund['msg'] == 'Success'))) {

      return true;
    }
    return false;
  }
}